package com.mm.exception;

import com.mm.enums.CodeMsgEnu;
import com.mm.statics.CodeMsg;
import com.mm.util.ResultUtil;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.IllegalBlockSizeException;
import java.util.List;

/**
 * @author 杨朝湖
 * @ClassName: GlobleExceptionHandler
 * @Description: 全局异常拦截类
 * @date 2020-11-06 09:24:35
 */
@ControllerAdvice
@RestController
public class GlobleExceptionHandler {

    /**
     * 数据校验异常拦截类
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultUtil<Object> exceptionHandler(MethodArgumentNotValidException e) {
        BindingResult result = e.getBindingResult();
        if (result.hasErrors()) {
            // 获取异常集合
            List<ObjectError> errors = result.getAllErrors();
            // 取出第一个异常拦截信息
            String message = errors.get(0).getDefaultMessage();
            // 返回信息
            return ResultUtil.error(CodeMsg.BIND_ERROR.fillArgs(message));
        }
        return ResultUtil.error(CodeMsgEnu.SERVER_ERROR);
    }

    /**
     * 全局异常处理拦截
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ResultUtil<Object> exceputionAll(Exception e) {
        e.printStackTrace();
        if (e instanceof HttpMessageNotReadableException) {
            return ResultUtil.error(CodeMsg.BIND_ERROR.fillArgs("传入参数异常"));
        } else if (e instanceof NullPointerException) {
            return ResultUtil.error(CodeMsg.BIND_ERROR.fillArgs("返回数据为空"));
        } else if (e instanceof CustomException) {// 自定义全局异常处理
            return ResultUtil.error(CodeMsg.BIND_ERROR.fillArgs(e.getMessage()));
        } else if (e instanceof IllegalBlockSizeException) {
            return ResultUtil.error(CodeMsg.BIND_ERROR.fillArgs("传入参数异常"));
        } else if (e instanceof UnknownAccountException) {//不存在该用户
            return ResultUtil.error(CodeMsgEnu.NO_USER);
        } else if (e instanceof IncorrectCredentialsException) {//用户名或密码错误
            return ResultUtil.error(CodeMsgEnu.USER_NOT);
        } else if (e instanceof UnauthenticatedException) {//未登录
            return ResultUtil.error(CodeMsgEnu.NOT_LOGGED_IN);
        } else if (e instanceof AuthorizationException) {//没权限
            return ResultUtil.error(CodeMsgEnu.NO_PERMISSION);
        }
        e.printStackTrace();
        return ResultUtil.error(CodeMsgEnu.NOT_KNOWS_ERR);
    }

}
